package com.example.reference_book.service.impl;

import com.example.reference_book.entity.BookQuality;
import com.example.reference_book.dao.BookQualityDao;
import com.example.reference_book.service.BookQualityService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * 书籍质量表(BookQuality)表服务实现类
 *
 * @author makejava
 * @since 2022-06-07 23:08:10
 */
@Service("bookQualityService")
public class BookQualityServiceImpl implements BookQualityService {
    @Resource
    private BookQualityDao bookQualityDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public BookQuality queryById(Integer id) {
        return this.bookQualityDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param bookQuality 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<BookQuality> queryByPage(BookQuality bookQuality, PageRequest pageRequest) {
        long total = this.bookQualityDao.count(bookQuality);
        return new PageImpl<>(this.bookQualityDao.queryAllByLimit(bookQuality, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param bookQuality 实例对象
     * @return 实例对象
     */
    @Override
    public BookQuality insert(BookQuality bookQuality) {
        this.bookQualityDao.insert(bookQuality);
        return bookQuality;
    }

    /**
     * 修改数据
     *
     * @param bookQuality 实例对象
     * @return 实例对象
     */
    @Override
    public BookQuality update(BookQuality bookQuality) {
        this.bookQualityDao.update(bookQuality);
        return this.queryById(bookQuality.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.bookQualityDao.deleteById(id) > 0;
    }
}
